package commands;

import java.util.ArrayList;

import twitter4j.internal.org.json.JSONArray;
import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;
import database.models.TagModel;

/**
 * Command that gets the top 10 tags from the database.
 * 
 * @author Project group 9
 * 
 */
public class GetTopTagsCommand implements Command {
	JSONObject jsonObject;
	TagModel tagModel;

	public GetTopTagsCommand(JSONObject jsonObject, TagModel tagModel) {
		this.jsonObject = jsonObject;
		this.tagModel = tagModel;
	}

	@Override
	public String execute() throws JSONException {
		JSONArray sendArray = new JSONArray();

		JSONArray parameters = jsonObject.getJSONArray("getTopTags");

		ArrayList<ArrayList> tags;

		if (parameters.isNull(0)) {
			tags = tagModel.getTopTags();
		} else {
			tags = tagModel.getTopTags(parameters);
		}

		for (ArrayList tag : tags) {
			JSONObject arrayObject = new JSONObject();
			arrayObject.put("name", tag.get(0));
			arrayObject.put("total", tag.get(1));
			sendArray.put(arrayObject);
		}
		
		return sendArray.toString();
	}
}
